/*
 *  CEMenuItemSprite.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CEMenuItemSprite.h"


using namespace cocos2d;

namespace cocosext {



/////////////////////////////////////////////////////
//
/** CEMenuItemSprite 
* 
*/
/////////////////////////////////////////////////////

CEMenuItemSprite * CEMenuItemSprite::itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
    return CEMenuItemSprite::itemFromNormalSprite(normalSprite, selectedSprite, disabledSprite, NULL, NULL);
}
CEMenuItemSprite * CEMenuItemSprite::itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
{
    return CEMenuItemSprite::itemFromNormalSprite(normalSprite, selectedSprite, NULL, target, selector);
}
CEMenuItemSprite * CEMenuItemSprite::itemFromNormalSprite(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
{
    CEMenuItemSprite *pRet = new CEMenuItemSprite();
    pRet->initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector); 
    pRet->autorelease();
    return pRet;
}

/////////////////////////////////////////////////////
//

CEMenuItemSprite::~CEMenuItemSprite() 
{

}
  
bool CEMenuItemSprite::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector)
{
	if (!CCMenuItemSprite::initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector))
		return false;

	_pNormalImage = dynamic_cast<CCRGBAProtocol*>(normalSprite);
	CCAssert(_pNormalImage != NULL, "CEMenuItemSprite::itemFromNormalSprite: normalSprite must support CCRGBAProtocol!");

	_pSelectedImage = dynamic_cast<CCRGBAProtocol*>(selectedSprite);
	CCAssert(_pSelectedImage != NULL, "CEMenuItemSprite::itemFromNormalSprite: selectedSprite must support CCRGBAProtocol!");
		
	
	if (disabledSprite != NULL)
	{
		_pDisabledImage = dynamic_cast<CCRGBAProtocol*>(disabledSprite);
		CCAssert(_pDisabledImage != NULL, "CEMenuItemSprite::itemFromNormalSprite: disabledSprite must support CCRGBAProtocol!");
	}

	_pSelectedImage->setOpacity(0);
//	_pSelectedImage->setIsVisible(true);

	return true;
}

/////////////////////////////////////////////////////
// Main interface

void CEMenuItemSprite::release(void)
{
	CCMenuItemSprite::release();
}

void CEMenuItemSprite::retain(void)
{
	CCMenuItemSprite::retain();
}

CCAction* CEMenuItemSprite::runAction(CCAction* action)
{
	return CCMenuItemSprite::runAction(action);
}

void CEMenuItemSprite::stopAllAdditionalActions()
{
	if (m_pSelectedImage)
		m_pSelectedImage->stopAllActions();
	if (m_pNormalImage)
		m_pNormalImage->stopAllActions();
}

/////////////////////////////////////////////////////
// public Cocos2d interface

void CEMenuItemSprite::selected()
{
    CCMenuItem::selected();
    
    if (m_pDisabledImage)
    {
        m_pDisabledImage->setIsVisible(false);
    }

    if (m_pSelectedImage)
    {
		this->getParent()->reorderChild(this, this->getZOrder() + 1); 

		CCFadeTo* fadeIn = CCFadeTo::actionWithDuration(CE_MENUITEMSPRITE_FADE_IN_TIME*(float)(255 - _pSelectedImage->getOpacity())/255.f, 255);
		m_pSelectedImage->stopAllActions();
		m_pSelectedImage->setIsVisible(true);
		m_pSelectedImage->runAction(fadeIn);

		CCFadeTo* fadeOut = CCFadeTo::actionWithDuration(CE_MENUITEMSPRITE_FADE_IN_TIME*(float)_pNormalImage->getOpacity()/255.f, 0);
		m_pNormalImage->stopAllActions();
		m_pNormalImage->runAction(CCSequence::actions(fadeOut, CCHide::action(), NULL));
    }
    else
    {
        m_pNormalImage->setIsVisible(true);
    }
}

void CEMenuItemSprite::unselected()
{
    CCMenuItem::unselected();
    
	CCFadeTo* fadeIn = CCFadeTo::actionWithDuration(CE_MENUITEMSPRITE_FADE_OUT_TIME*(float)(255 - _pNormalImage->getOpacity())/255.f, 255);
	m_pNormalImage->stopAllActions();
	m_pNormalImage->setIsVisible(true);
	m_pNormalImage->runAction(fadeIn);
    
    if (m_pSelectedImage)
    {
//		CCFadeTo* fadeOut = CCFadeTo::actionWithDuration(CE_MENUITEMSPRITE_FADE_OUT_TIME*(float)_pSelectedImage->getOpacity()/255.f, 0);
		CCFadeOut* fadeOut = CCFadeOut::actionWithDuration(CE_MENUITEMSPRITE_FADE_OUT_TIME);
		m_pSelectedImage->stopAllActions();
		m_pSelectedImage->runAction(CCSequence::actions(fadeOut, CCHide::action(), CCCallFunc::actionWithTarget(this, callfunc_selector(CEMenuItemSprite::decreaseZOrder)), NULL));
    }
    
    if (m_pDisabledImage)
    {
        m_pDisabledImage->setIsVisible(false);
    }
}

void CEMenuItemSprite::decreaseZOrder()
{
	this->getParent()->reorderChild(this, this->getZOrder() - 1);
}



/////////////////////////////////////////////////////
//
/** CEMenuItemSpriteWithText 
* 
*/
/////////////////////////////////////////////////////

CEMenuItemSpriteWithText* CEMenuItemSpriteWithText::itemFromNormalSprite(cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
																		 cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
																		 cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
																		 cocos2d::CCObject* target, 
																		 cocos2d::SEL_MenuHandler selector)
{
    CEMenuItemSpriteWithText *pRet = new CEMenuItemSpriteWithText();
    pRet->initFromNormalSprite(normalSpriteWithLabel, pNormalLabel,
							   selectedSpriteWithLabel, pSelectedLabel, 
							   disabledSpriteWithLabel, pDisabledLabel, 
							   target, 
							   selector); 
    pRet->autorelease();
    return pRet;
}

/////////////////////////////////////////////////////
// Constructor, destructor and init
  
bool CEMenuItemSpriteWithText::initFromNormalSprite(cocos2d::CCNode* normalSpriteWithLabel, CELabelProtocol* pNormalLabel,
													cocos2d::CCNode* selectedSpriteWithLabel, CELabelProtocol* pSelectedLabel, 
													cocos2d::CCNode* disabledSpriteWithLabel, CELabelProtocol* pDisabledLabel, 
													cocos2d::CCObject* target, 
													cocos2d::SEL_MenuHandler selector)
{
	if (!CEMenuItemSprite::initFromNormalSprite(normalSpriteWithLabel, selectedSpriteWithLabel, disabledSpriteWithLabel, target, selector))
		return false;

	_pNormalLabel = pNormalLabel;
	_pSelectedLabel = pSelectedLabel;
	_pDisabledLabel = pDisabledLabel;

	return true;
}

/////////////////////////////////////////////////////
//  CELabelProtocol interface 

void CEMenuItemSpriteWithText::setString(const wchar_t *str)
{
	_pNormalLabel->setString(str);
	_pSelectedLabel->setString(str);
	if (_pDisabledLabel != NULL)
		_pDisabledLabel->setString(str);
}

const wchar_t* CEMenuItemSpriteWithText::getString(void) const
{
	return _pNormalLabel->getString();
}

}; // end namespace cocosext

